﻿using App.Common;
using QxFramework.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseManager : MonoSingleton<MouseManager>
{

    public StateMachine<MouseManager> stateMachine;

    public MouseIdleState _idleState;

    public MouseInMapState _inMapState;

    public MouseOutOfMapState _outOfState;

    public Vector2 _mouseWorldPos=Vector2.zero;

    public bool isMouseDown = false;

    [HideInInspector]
    public CardObject _bindCardObject;

    [HideInInspector]
    public Vector3 _preNearestPos = new Vector3(0, 0, 0);

    /// <summary>
    /// 当前是否用鼠标拿起卡片
    /// </summary>
    [HideInInspector]
    public bool isCardWithMouse = false;

    /// <summary>
    /// 按着空格键(进入预览状态)
    /// </summary>
    [HideInInspector]
    public bool isPressedSpace = false;

    public Camera camera;

    public string nowStateName;

    private void Start()
    {
        stateMachine = new StateMachine<MouseManager>(this);

        _inMapState = new MouseInMapState();

        _outOfState = new MouseOutOfMapState();

        _idleState = new MouseIdleState();

        stateMachine.CurrentState = _idleState;

    }

    private void Update()
    {
        stateMachine.Update();

        //camera = Camera.current;

        if (camera != null) 
        {
            _mouseWorldPos = camera.ScreenToWorldPoint(Input.mousePosition);
        }

        isMouseDown = Input.GetMouseButton(0);

        isPressedSpace = Input.GetKey(KeyCode.Space);

        try
        {
            _preNearestPos = Maps.Instance.GetNearestHexPos(_mouseWorldPos);
        }catch(Exception e)
        {
            Debug.LogWarning("关卡加载中");
        }

    }

    //点击
    public void OnClickedCards(Card card)
    {
        if (!isCardWithMouse)
        {
            isCardWithMouse = true;
            _bindCardObject = card._bindObject;
            if (_bindCardObject != null) 
            {
                _bindCardObject = Instantiate(_bindCardObject, GameObject.Find("Instantiate").transform);
            }
            else
            {
                Debug.Log("bindNull");
            }
            ChangeState(_outOfState);
        }

    }

    public void ChangeState(State<MouseManager> newState)
    {
        stateMachine.ChangeState(newState);
        //方便调试
        nowStateName = newState.GetType().Name;
    }

    public void ResetCardPool()
    {
        if (isCardWithMouse)
        {
            isCardWithMouse = false;
           _bindCardObject = null;
            CardManager.Instance.UpdateCards(_bindCardObject);
        }
    }

    public void Fixed(Vector3 startPos)
    {
        Debug.LogWarning("Fixed");

        //Fixed
        _bindCardObject.transform.position = Data.Instance.getWorldPos(startPos);
        //

    }

    public void RefreshCards(bool isLay)
    {
        if (!isLay)
        {
            CardManager.Instance.UpdateCards(null);
        }
        else
        {
            CardManager.Instance.UpdateCards(_bindCardObject);
        }
        Destroy(_bindCardObject.gameObject);
    }
}

public class MouseIdleState : State<MouseManager>
{
    public override void Enter(MouseManager miner, object args = null)
    {
    }

    public override void Execute(MouseManager miner)
    {
        //Debug.Log("idle");
    }

    public override void Exit(MouseManager miner)
    {
    }
}

public class MouseInMapState : State<MouseManager>
{
    public bool isChanged = false;
    public override void Enter(MouseManager miner, object args = null)
    {
        isChanged = false;
    }

    public override void Execute(MouseManager miner)
    {
        if (MouseManager.Instance.isCardWithMouse)
        {
            Vector3 nearest = Maps.Instance.GetNearestHexPos(miner._mouseWorldPos);
            if (Maps.Instance.IsOK2Lay(miner._bindCardObject, nearest))
            {
                //当前处于预览状态
                if (MouseManager.Instance.isPressedSpace)
                {

                    
                    //和上一次位置不同就回滚上一次的操作
                    if ((Vector2)miner._bindCardObject.transform.position != Data.Instance.getWorldPos(nearest))
                    {
                        Debug.Log("putin");
                        Maps.Instance.Revert(miner._bindCardObject);
                        isChanged = false;
                    }

                    //这次操作未落实就落实这次操作
                    if (!isChanged)
                    {
                        Maps.Instance.DisPlay(miner._bindCardObject, nearest, false);
                        miner._bindCardObject.DynasticChangeSlots();
                        isChanged = true;
                    }
                    GameObject.Find("Map").GetComponent<LightCheck>().StartCheck(false);
                    //按下左键放置卡牌
                    if (Input.GetMouseButton(0))
                    {
                        miner.RefreshCards(true);
                        MouseManager.Instance.isCardWithMouse = false;
                        miner.ChangeState(miner._idleState);
                        GameObject.Find("Map").GetComponent<LightCheck>().StartCheck(true);
                        Maps.Instance.OperationThings.Clear();
                        return;
                    }
                }
                //若非预览状态就回滚当前操作
                else
                {
                    
                    if (isChanged)
                    {
                        Maps.Instance.Revert(miner._bindCardObject);
                        isChanged = false;
                    }
                    GameObject.Find("Map").GetComponent<LightCheck>().StartCheck(false);
                }
                //修改框的位置
                miner._bindCardObject.transform.position = Data.Instance.getWorldPos(nearest);
            }
            else
            {
                miner._bindCardObject.transform.position = MouseManager.Instance._mouseWorldPos;
                GameObject.Find("Map").GetComponent<LightCheck>().StartCheck(false);
            }

            //按下右键卡牌就返回卡池
            if (Input.GetMouseButton(1))
            {
                miner.RefreshCards(false);
                MouseManager.Instance.isCardWithMouse = false;
                miner.ChangeState(miner._idleState);
                GameObject.Find("Map").GetComponent<LightCheck>().StartCheck(true);
                Maps.Instance.OperationThings.Clear();
                return;
            }
        }
        //鼠标移出界就切换状态
        if (!Maps.Instance.ExistPanel(miner._mouseWorldPos))
        {
            Maps.Instance.Revert(miner._bindCardObject);
            miner.ChangeState(miner._outOfState);
            GameObject.Find("Map").GetComponent<LightCheck>().StartCheck(false);
            isChanged = false;
            return;
        }
    }

    public override void Exit(MouseManager miner)
    {
        isChanged = false;
    }
}

public class MouseOutOfMapState : State<MouseManager>
{
    public override void Enter(MouseManager miner, object args = null)
    {

    }

    public override void Execute(MouseManager miner)
    {
        //没选中卡片不更新
        if (MouseManager.Instance.isCardWithMouse)
        {
            //设置位置跟着鼠标走
            miner._bindCardObject.transform.position = miner._mouseWorldPos;

            //Debug.Log("OutOf");
            //鼠标进入地图
            if (Maps.Instance.ExistPanel(miner._mouseWorldPos))
            {
                miner.ChangeState(miner._inMapState);

                return;
            }
            //松手(点击右键)
            if (Input.GetMouseButton(1)) 
            {
                miner.RefreshCards(false);
                miner.ChangeState(miner._idleState);
                return;
            }
        }

    }

    public override void Exit(MouseManager miner)
    {
    }
}
